﻿/*
 * Copyright (c) 2021-2031, hhfang(240970166@qq.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#pragma once 
#include "hdefine.h"
typedef enum
{
    LOG_DEBUG = 0,
    LOG_INFO,
    LOG_WARNING,
    LOG_ERROR
} e_log_level;
typedef enum
{
    NET_OBJECT_CONNECTION = 0,
    NET_OBJECT_CONNECTOR4,
    NET_OBJECT_CONNECTOR6,
    NET_OBJECT_ACCEPTOR4,
    NET_OBJECT_ACCEPTOR6,
} net_object_type;

typedef enum
{
    HNET_EV_ACT = 0,
    HNET_EV_PAS,
    HNET_EV_MAX,
}hnet_ev;

typedef enum
{
    HNET_RV_GOOD = 0,
    HNET_RV_BAD,
    HNET_RV_MAX,
}hnet_rv;

typedef void (*pfun_ev)(hnet_ev, const char*, connection_id, const char*, int, const char*, int);
typedef void (*pfun_rv)(hnet_rv, connection_id, const char*, int);

typedef int hsize;
struct hbuffer;

typedef struct
{
    pfun_ev ev;
    pfun_rv rv;
}hnet_ev_rv, * phnet_ev_rv;

#ifdef __cplusplus
extern "C" {
#endif
    int get_current_dir(char* buf, int len);
    int is_dir_exist(const char* path);
    int create_dir_recursive(const char* path);
    int remove_dir_recursive(const char* path);
    void init_hlog(e_log_level min_allowed_log_level, const char* path);
    void uninit_hlog();
    void hlog(const char* tag, e_log_level, int console1, const char* fmt, ...);
    struct hbuffer* malloc_hbuffer(hsize size);
    void free_hbuffer(struct hbuffer* phb);
    hsize write_data(struct hbuffer* phb, const char* data, hsize len);
    void advance_write_pos(struct hbuffer* phb, hsize pos);
    void advance_read_pos(struct hbuffer* phb, hsize pos);
    char* get_readable_pos(struct hbuffer* phb);
    char* get_writeable_pos(struct hbuffer* phb);
    hsize get_readable_num(struct hbuffer* phb);
    hsize get_writeable_num(struct hbuffer* phb);
    void init_hnet(int sbuf, int rbuf);
    void uninit_hnet();
    connection_id add_connector(phnet_ev_rv pcb, const char* cid, const char* ip, int port, int b4true6false, int reconnect_gap);
    connection_id add_acceptor(phnet_ev_rv pcb, const char* aid, const char* ip, int port, int b4true6false);
    void stop_acceptor(const char* aid);
    void stop_connector(const char* cid);
    void send_data(connection_id cid, const char* pdata, int len);
    void close_connection(connection_id cid);
    int run_hnet();
    void post_hb();
    void test_malloc();
#ifdef __cplusplus
}
#endif

//#ifdef _DEBUG
//#define HLOG_DEBUG(fmt, ...)  hlog("hlog" ,LOG_DEBUG,1,fmt, ##__VA_ARGS__)
//#define HLOG_INFO(fmt,  ...)  hlog("hlog" ,LOG_INFO,1,fmt, ##__VA_ARGS__)
//#define HLOG_WARN(fmt,  ...)  hlog("hlog" ,LOG_WARNING,1,fmt, ##__VA_ARGS__)
//#define HLOG_ERROR(fmt, ...)  hlog("hlog" ,LOG_ERROR,1,fmt, ##__VA_ARGS__)
//#else
#define HLOG_DEBUG(fmt, ...)  
#define HLOG_INFO(fmt,  ...)  
#define HLOG_WARN(fmt,  ...)  
#define HLOG_ERROR(fmt, ...)   
//#endif
